package cn.hd01.web.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hd01.common.BaseConstant.Status;
import cn.hd01.common.BaseStatus;
import cn.hd01.common.OperationStatus;
import cn.hd01.repository.entity.COrder;
import cn.hd01.repository.entity.Customer;
import cn.hd01.repository.entity.CustomerOperation;
import cn.hd01.repository.entity.Product;
import cn.hd01.service.COrderService;
import cn.hd01.service.CustomerOperationService;
import cn.hd01.service.CustomerService;
import cn.hd01.service.OrderProductService;
import cn.hd01.service.ProductService;
import cn.hd01.web.auth.Auth;

@Controller
@RequestMapping("/customerOperation")
@Auth
public class CustomerOperationController {
	
	@Autowired
	private CustomerOperationService customerOperationService;
	
	@Autowired
	private COrderService cOrderService;
	
	@Autowired
	private ProductService productService;
	
	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private OrderProductService orderProductService;
	
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	@RequestMapping(method = RequestMethod.GET)
	public String index(Model m, HttpServletRequest request) {
		Integer orderId = StringUtils.isEmpty(request.getParameter("orderId")) ? null : Integer.valueOf(request.getParameter("orderId"));
		m.addAttribute("orderId", orderId);
		m.addAttribute("OperationStatusList", Arrays.asList(OperationStatus.values()));
		m.addAttribute("status", request.getParameter("status"));
		if(orderId != null){
			m.addAttribute("productList", orderProductService.findByOrderId(orderId));
		}else{
			m.addAttribute("productList", orderProductService.findAll());
		}
		
		m.addAttribute("presentStatusList", Arrays.asList(BaseStatus.values()));
		Date today = new Date();
		m.addAttribute("today", sdf.format(today));
		if(orderId != null){
			COrder order = cOrderService.findOne(orderId);
			m.addAttribute("customerName", order.getCustomerName());
		}else{
			List<Object[]> customerList = customerService.findAllCustomer();
			m.addAttribute("customerList", customerList);
		}
		Calendar c = Calendar.getInstance();  
	    c.setTime(today);  
	    c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天  
	   
	    Date tomorrow = c.getTime();
	    m.addAttribute("tomorrow", sdf.format(tomorrow));
	    
		return "customerOperation";
	}
	
	@ResponseBody
	@RequestMapping(path = "/list", method = RequestMethod.GET)
	private Page<CustomerOperation> find(CustomerOperation operation, @PageableDefault Pageable pageable, HttpServletRequest req) {
		return customerOperationService.findAll(operation, pageable); 
	}
	
	@ResponseBody
	@RequestMapping(path = "/delete/{id}", method = RequestMethod.GET)
	private void delete(@PathVariable("id") Integer id) {
		customerOperationService.delete(id);
	}
	
	@ResponseBody
	@RequestMapping(path = "/save", method = RequestMethod.POST)
	private void save(CustomerOperation customerOperation) {
		COrder order = null;
		if(customerOperation.getOrderId() != null){
			order = cOrderService.findOne(customerOperation.getOrderId());
			customerOperation.setCustomerName(order.getCustomerName());
			customerOperation.setCustomerPhoneNo(order.getCustomerPhoneNo());
		}else{
			Customer customer = customerService.findOne(customerOperation.getCustomerId());
			customerOperation.setCustomerName(customer.getName());
			customerOperation.setCustomerPhoneNo(customer.getPhoneNo());
		}
		
		String[] productIds = customerOperation.getProductId().split(",");
		StringBuffer sb = new StringBuffer();
		for(String productId : productIds){
			Product product = productService.findOne(Integer.valueOf(productId));
			sb.append(product.getName()).append(",");
		}
		customerOperation.setProductName(sb.substring(0, sb.length() - 1));
		
		if(customerOperation.getId() == null){
			customerOperation.setStatus(Status.INVALID);
		}
		
		customerOperationService.save(customerOperation);
	}
	
	@ResponseBody
	@RequestMapping(path = "/down/{id}", method = RequestMethod.GET)
	private void down(@PathVariable("id") Integer id) {
		CustomerOperation oper = customerOperationService.findOne(id);
		oper.setStatus(Status.VALID);
		customerOperationService.save(oper);
	}

}
